﻿using System;
using System.Collections.Generic;
using System.Text;

using Mang24k99.Model.Dissertation;
using System.Data;
using Castle.ActiveRecord.Queries;

namespace Mang24k99.Business.Dissertation
{
    public partial class BDissertations
    {
        /// <summary>
        /// 添加一个专题
        /// </summary>
        /// <param name="dis">专题实体对象</param>
        public static void Add(MDissertations dis)
        {
            dis.Create();
        }

        /// <summary>
        /// 根据网站的标识获取该网站下专题对应的ID与名称集合
        /// </summary>
        /// <param name="disID">网站ID集合</param>
        /// <param name="isOnTop">是否是专题前五个</param>
        /// <returns></returns>
        public static IList<Dissert> FindDissertations(int webID,int iStart,int iEnd,out int iCount,bool isOnTop)
        {
            string[] disID = GetDisID(webID.ToString()); //获取该网站的所有专题ID
            IList<Dissert> disName=new List<Dissert>();
            iCount = disID.Length;
            for (int i = iStart; i < (disID.Length > iEnd ? iEnd : disID.Length);i++ )
            {
                Dissert dis=new Dissert();
                dis.ID=disID[i];
                dis.Name = MDissertations.Find(disID[i]);
                dis.Number = MDissertations.Find(int.Parse(disID[i])).DissertationNumber;
                if (isOnTop && dis.Number < 6)
                    disName.Add(dis);
                else if(!isOnTop)
                    disName.Add(dis);
            }
            return disName;
        }

        /// <summary>
        /// 查询专题,分为查询上线专题与所有专题
        /// </summary>
        /// <param name="webID"></param>
        /// <param name="isOnTop"></param>
        /// <returns></returns>
        public static IList<Dissert> FindDissertations(int webID, bool isOnTop)
        {
            string[] disID = GetDisID(webID.ToString()); //获取该网站的所有专题ID
            IList<Dissert> disName = new List<Dissert>();
            foreach (string id in disID)
            {
                Dissert dis = new Dissert();
                dis.ID = id;
                try
                {
                    string name = MDissertations.Find(id);
                    dis.Name = name.Length > 30 ? name.Substring(0, 30) + "..." : name;
                    dis.Number = MDissertations.Find(int.Parse(id)).DissertationNumber;

                }
                catch
                {

                }
                    if (isOnTop && dis.Number < 26)
                        disName.Add(dis);
                    else if (!isOnTop)
                        disName.Add(dis);
               
               
              
            }
            return disName;
        }

        /// <summary>
        /// 查询已经上线专题
        /// </summary>
        /// <param name="webID"></param>
        /// <param name="isOnTop"></param>
        /// <returns></returns>
        public static IList<Dissert> FindDissertations(int webID)
        {
            string[] disID = GetDisID(webID.ToString()); //获取该网站的所有专题ID
            IList<Dissert> disName = new List<Dissert>();
            foreach (string id in disID)
            {
                Dissert dis = new Dissert();
                dis.ID = id;
                MDissertations adis = MDissertations.Find(int.Parse(id));
                try
                {
                    dis.Name = MDissertations.Find(id);
                    dis.Number = adis.DissertationNumber;
                    if (adis.DissertationOnline == 1)
                        disName.Add(dis);
                }
                catch
                {

                }
                
            }
            return disName;
        }

        /// <summary>
        /// 当删除一个专题后,让后面的专题接替上来
        /// </summary>
        /// <param name="disID"></param>
        /// <returns></returns>
        public static bool SetLastDisdertation(int disID)
        {
            try
            {
                MDissertations[] allDis = MDissertations.FindAll();
                foreach (MDissertations aDis in allDis)
                {
                    if (aDis.DissertationNumber < 26 && aDis.DissertationNumber > disID)
                    {
                        aDis.DissertationNumber--;
                        aDis.Update();
                    }
                }
                return true;
            }
            catch { return false; }
        }

        /// <summary>
        /// 获取该网站下的专题ID集合
        /// </summary>
        /// <param name="webID">网站ID标识</param>
        /// <returns></returns>
        public static string[] GetDisID(string webID)
        {
            IList<MDissertations> mDiss = MDissertations.Find("DissertationWeb", int.Parse(webID));
            string[] disID=new string[mDiss.Count];
            int i=0;
            foreach (MDissertations mDis in mDiss)
            {
                disID[i++] = mDis.DissertationID.ToString();
            }
            return disID;
        }

        /// <summary>
        /// 获取该网站下所有的专题ID集合
        /// </summary>
        /// <param name="webID">网站ID标识</param>
        /// <returns></returns>
        public static string[] GetDisID(string webID, bool all)
        {
            if (all)
            {
                IList<MDissertations> mDiss = MDissertations.Find(webID,true);
                string[] disID = new string[mDiss.Count];
                int i = 0;
                foreach (MDissertations mDis in mDiss)
                {
                    disID[i++] = mDis.DissertationID.ToString();
                }
                return disID;
            }
            else
                return null;
        }

        /// <summary>
        /// 设置专题顺序
        /// </summary>
        /// <param name="olddisID">先前设置的前五个专题</param>
        /// <param name="newdisID">新设置的前五个专题</param>
        public static void ConfigDis(string[] olddisID,string[] newdisID)
        {
            //int[] index = new int[newdisID.Length];
            //for (int i = 0; i < newdisID.Length;i++)
            //{
            //    MDissertations aDis = MDissertations.Find(int.Parse(newdisID[i]));
            //    index[i] = aDis.DissertationNumber;//记录新排专题之前的顺序
            //    aDis.DissertationNumber = i+1;//专题顺序还原
            //    MDissertations.Update(aDis);
            //}
            //int indexs = new int[newdisID.Length];
            //for (int i = 0; i < newdisID.Length;i++ )
            //{
            //    MDissertations oldDis = MDissertations.Find(int.Parse(olddisID[i]));
            //    if(oldDis.DissertationNumber>6)
            //        oldDis.DissertationNumber = index[i];
            //    MDissertations.Update(oldDis);
            //}


            for (int i = 0; i < newdisID.Length; i++)
            {
                MDissertations aDis = MDissertations.Find(int.Parse(newdisID[i]));
                int index = aDis.DissertationNumber;//记录新排专题之前的顺序
                aDis.DissertationNumber = i + 1;//专题顺序还原
                MDissertations.Update(aDis);

                if (index > newdisID.Length)//
                {
                    MDissertations oldDis = MDissertations.Find(int.Parse(olddisID[i]));
                    oldDis.DissertationNumber = index;
                    MDissertations.Update(oldDis);
                }
            }
        }

        /// <summary>
        /// 设置该专题上线
        /// </summary>
        /// <param name="webID"></param>
        /// <param name="disID"></param>
        public static void SetDisOnline(string webID,string disID)
        {
            string[] disOnline = GetDisID(webID);
            if(disOnline.Length>0)
            {
                for (int i = 0; i < disOnline.Length; i++)
                {
                    MDissertations aDis = MDissertations.Find(int.Parse(disOnline[i]));
                    if (aDis.DissertationNumber < 26)//只允许25个专题
                    {
                        if (aDis.DissertationNumber == 25)
                            aDis.DissertationOnline = 0;
                        aDis.DissertationNumber = aDis.DissertationNumber + 1;// > 5 ? 0 : aDis.DissertationNumber + 1;

                        aDis.UpdateTime = DateTime.Now; 
                        MDissertations.Update(aDis);
                    }
                    //}
                }
            }
            MDissertations aDiss = MDissertations.Find(int.Parse(disID));
            if (aDiss != null)
            {
                aDiss.DissertationNumber = 1;
                aDiss.DissertationOnline = 1;
                aDiss.UpdateTime = DateTime.Now;
                MDissertations.Update(aDiss);
            }
        }

        /// <summary>
        /// 查询最大的专题顺序ID
        /// </summary>
        /// <param name="webID"></param>
        /// <returns></returns>
        public static int GetMaxDissertationNumber(string webID)
        {
            return GetMaxDissertationNumber(webID);
        }

        /// <summary>
        /// 获取该网站下所有的专题及其与专题名称的新表
        /// </summary>
        /// <param name="webID">网站ID</param>
        /// <returns></returns>
        public static DataTable FindAllList(string webID,int iStart,int iEnd,out int iCount,string filter)
        {
            DataTable dt = new DataTable();
            int index = 1;
            iCount = 0;
            dt.Columns.Add("ID", typeof(int));//索引
            dt.Columns.Add("Name", typeof(string));//专题名称
            dt.Columns.Add("DisNumber", typeof(int));//专题ID
            dt.Columns.Add("IsUpLine", typeof(int));//是否上线
            dt.Columns.Add("CreateTime", typeof(DateTime));
            dt.Columns.Add("Content", typeof(string));
            dt.Columns.Add("DisID", typeof(int));//专题ID
            IList<MDissertations> dtDissertation = MDissertations.Find("DissertationWeb", int.Parse(webID));
            if (dtDissertation != null)
            {
                foreach (MDissertations aDis in dtDissertation)
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = index++;
                    try
                    {
                        MColumnToNewsTable mctt = MColumnToNewsTable.Find(aDis.DissertationID);
                        dr[1] = mctt.NewsTitle.Substring(0, 5) + "...";
                        dr[2] = aDis.DissertationNumber;
                        dr[3] = aDis.DissertationOnline;
                        MNewsContentTable mnct = MNewsContentTable.Find(mctt.NewsID);
                        dr[4] = aDis.CreateTime;
                        dr[5] = mnct.NewsContent_Text.Length > 10 ? mnct.NewsContent_Text.Substring(0, 10) + "..." : mnct.NewsContent_Text;
                        dr[6] = aDis.DissertationID;
                        dt.Rows.Add(dr);
                    }
                    catch { index--; }
                }
                DataTable nTd = dt.Clone();
                if (filter == null)//查找记录
                    filter = "1=1";
                DataRow[] drs = dt.Select(filter,"DisNumber asc,CreateTime desc");
                if (drs.Length > 0)
                {
                    iCount = drs.Length;
                    for (int i = iStart ; i < (drs.Length > iEnd ? iEnd : drs.Length); i++)
                    {
                        drs[i][0] = i + 1;
                        nTd.ImportRow(drs[i]);
                    }
                }
                //}
                //else
                //{
                //    for (int x = iStart - 1; x < (dtDissertation.Count > iEnd ? iEnd : dtDissertation.Count); x++)
                //    {
                //        nTd.ImportRow(dt.Rows[x]);
                //    }
                //}
                return nTd;
            }
            else
                return null;
        }
    }
}